<?

class Model {
    
    public function set($var,$val){
        $this->$var = $val;
    }
    public function get($var){
        return $this->$var;
    }
    public function fromArray($array){
    	foreach($this as $var=>$val){
    		if(isset($array[$var])){
    			$this->$var = $array[$var];
    		}
    	}
//        foreach($array as $var=>$val){
//            $this->$var = $val;
//        }
    }
    public function __toArray(){
      $vars = get_class_vars(get_class($this));
      foreach($vars as $name=>$null){
        $funcname = "get".$name;
        $return[$name] = $this->$funcname();
      }
      return $return;
    }
    public function getQueryInsert(){
      $updates = "";
      $aVars = get_class_vars(get_class($this));
      foreach($aVars as $name=>$null){
        $vars .= "$name,";
        $vals .= "\"".addslashes($this->$name)."\",";
        if($name != "_id"){
            $updates .= "$name=\"".addslashes($this->$name)."\",";
        }
        
      }
      $vars = substr($vars,0,-1);
      $vals = substr($vals,0,-1);
      $updates = substr($updates,0,-1);
      $query = "INSERT INTO ".strtolower(get_class($this))." ($vars) VALUES ($vals) ON DUPLICATE KEY UPDATE $updates;";
      return $query;
    }
    
}
